using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Visuals;
using FilteredStatistics.Controller;
using System.Diagnostics;

namespace FilteredStatistics.View
{
    partial class FilterResultsForm : Form
    {
        public FilterResultsForm(StatisticsTreeListDistributor distributor)
        {
            InitializeComponent();

            this.Text = PluginView.GetLocalizedString("FilteredStatisticsText");
            this.Location = Options.Instance.ResultsWindowPosition;
            this.Size = Options.Instance.ResultsWindowSize;

            m_Distributor = distributor;
            m_Distributor.RegisterStatisticsTreeList(StatisticsTreeList);

            m_bInitializing = false;
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            m_Distributor.UnregisterStatisticsTreeList(StatisticsTreeList);
        }

        protected override void OnMove(EventArgs e)
        {
            base.OnMove(e);

            if (!m_bInitializing)
            {
                Options.Instance.ResultsWindowPosition = this.Location;
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            if (!m_bInitializing)
            {
                Options.Instance.ResultsWindowSize = this.Size;
            }
        }

        private void StatisticsTreeList_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu menu = new ContextMenu();
                IconizedSTMenuItem menuItem;

                menuItem = new IconizedSTMenuItem(PluginView.GetLocalizedString("ListSettingsText"),
                                                new EventHandler(ListSettingsMenuEventHandler),
                                                CommonResources.Images.Table16, null);
                menu.MenuItems.Add(menuItem);

                menu.Show(this, PointToClient(MousePosition));
            }
        }

        private void StatisticsTreeList_ColumnClicked(object sender, TreeList.ColumnEventArgs e)
        {
            TreeListColumnInfo columnInfo = Options.Instance.StatisticsColumnsManager.FindColumn(new Guid(e.Column.Id));

            Debug.Assert(columnInfo != null);

            bool isAscending = columnInfo.Sorting == TreeListColumnInfo.ColumnSortingType.Ascending;

            if (isAscending)
            {
                Options.Instance.StatisticsColumnsManager.SetColumnSorting(new Guid(e.Column.Id), TreeListColumnInfo.ColumnSortingType.Descending);
            }
            else
            {
                Options.Instance.StatisticsColumnsManager.SetColumnSorting(new Guid(e.Column.Id), TreeListColumnInfo.ColumnSortingType.Ascending);
            }
        }

        private void StatisticsTreeList_ColumnResized(object sender, TreeList.ColumnEventArgs e)
        {
            Options.Instance.StatisticsColumnsManager.SetColumnWidth(new Guid(e.Column.Id), e.Column.Width);
        }

        private void StatisticsTreeList_SelectedChanged(object sender, EventArgs e)
        {
            m_Distributor.Selected = StatisticsTreeList.Selected;
        }

        public void ListSettingsMenuEventHandler(object sender, EventArgs args)
        {
            SelectColumnsForm dlg = new SelectColumnsForm(Options.Instance.StatisticsColumnsManager);

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Options.Instance.StatisticsColumnsManager.UpdateRegisteredColumns(dlg.VisibleColumns, dlg.HiddenColumns, dlg.FixedColumnCount);
            }
        }

        public IActivity Activity
        {
            set
            {
                StatisticsTreeList.Enabled = (value != null);
            }
        }

        StatisticsTreeListDistributor m_Distributor = null;
        bool m_bInitializing = true;
    }
}